//
//  WBMainViewController.swift
//  Weibo
//
//  Created by Apple on 16/11/12.
//  Copyright © 2016年 Wbw. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //自定义tabbar
        let tabBar = WBTabBar()
        //只读属性tabbar,用kvc来赋值
        self.setValue(tabBar, forKey: "tabBar")
        //监听点击事件
        //为闭包赋值
        tabBar.composeClosure = { [weak self] in
            //在实际开发中 按钮的点击事件处理的地方 需要留一个查找的标记
            print("撰写按钮被点击,在WBMainViewController中")
            print(self!)
        }
        addChildViewController()
        
    }
    //添加子视图控制器
    //private 前缀,私有方法,只能当前控制器调用
    private func addChildViewController() {
        addChildViewController(vc: WBHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(vc: WBMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(vc: WBDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(vc: WBProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
    }
    
    //MARK: 添加子视图控制器
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        //设置title
        vc.tabBarItem.title = title
        vc.navigationItem.title = title
        //设置标签的图片
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置标签的文字的颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: .selected)
        //设置文字的大小,必须设置默认状态的文字大小
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 10)], for: .normal)
        //设置气泡的颜色和个数
        vc.tabBarItem.badgeValue = "10"
        vc.tabBarItem.badgeColor = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)
        vc.tabBarItem.badgeValue = nil
        //将生成的控制器作为公共nav的根控制器
        let nav = WBBaseNaviController(rootViewController: vc)
        //将导航控制器添加到 UITabBarController对象的子视图控制器
        self.addChildViewController(nav)
        
    }
}
